package com.hpin.fansea.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.RadioAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.RadioResult;
import com.hpin.fansea.pulltorefush.PullToRefreshBase;
import com.hpin.fansea.pulltorefush.PullToRefreshListView;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 小区广播
 * Created by Aaron on 15/9/17.
 */
public class CommunityBroadcastActivity extends BaseActivity {

    private int currentPage = 0;
    private PullToRefreshListView plv_broadcast;
    private List<RadioResult.CellBroadcastList> radioLists;
    private RadioAdapter radioAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_community_broadcast);

        radioLists = new ArrayList<RadioResult.CellBroadcastList>();

        initWidget();

        getBroadcastData();

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.community_broadcast));

        iv_left.setOnClickListener(this);

        plv_broadcast = (PullToRefreshListView) findViewById(R.id.plv_broadcast);

        radioAdapter = new RadioAdapter(CommunityBroadcastActivity.this, radioLists);
        plv_broadcast.setAdapter(radioAdapter);

        plv_broadcast.setMode(PullToRefreshBase.Mode.BOTH);

        plv_broadcast.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                lastUpdate(refreshView);

                currentPage = 0;
                getBroadcastData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getBroadcastData();

                lastUpdate(refreshView);

            }
        });

        plv_broadcast.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                --position;

                Intent mIntent = new Intent(CommunityBroadcastActivity.this, BroadcastDetailActivity.class);

                RadioResult.CellBroadcastList cellBroadcastList = radioLists.get(position);
                mIntent.putExtra("broadcastDetail", cellBroadcastList);
                startActivity(mIntent);

            }
        });

    }

    /**
     * 获取小区广播数据
     */
    private void getBroadcastData() {

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.CELLBROADCASTLIST
                + "?token=" + sp.getString(Constant.token, "")
                + "&communityId=" + sp.getString(Constant.communityId, "") + "&pageSize="
                + Constant.PAGECOUNT + "&pageIndex=" + (++currentPage)
                + "&systemType=" + Constant.SYSTEM_TYPE, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {
                    plv_broadcast.onRefreshComplete();

                    RadioResult radioResult = JSONObject.parseObject(str, RadioResult.class);

                    if (Constant.STATUS_SUCESS.equals(radioResult.status)) {

                        setDataView(radioResult.result);
                        radioAdapter.setData(radioLists);
                        radioAdapter.notifyDataSetChanged();

                    } else {
                        showToast(radioResult.message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showToast("获取小区广播失败");
                }

            }
        }, new HttpUtil.FailListener() {
            @Override
            public void setOnFailListener() {
                plv_broadcast.onRefreshComplete();
            }
        });

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

        }

    }




    /**
     * 如果有更多则进行重复判断
     *
     * @param temp
     *            新的集合
     */
    protected void setDataView(List<RadioResult.CellBroadcastList> temp) {

        if (radioLists.size()==0) {
            radioLists = temp;
            return;
        }

        int nowSize = radioLists.size();
        int newSize = temp.size();
        for (int i = 0; i < newSize; i++) {
            boolean exist = true;
            RadioResult.CellBroadcastList mcod = temp.get(i);
            for (int j = 0; j < nowSize; j++) {
                RadioResult.CellBroadcastList nmcod = radioLists.get(j);
                if (mcod.id_.equals(nmcod.id_)) {
                    exist = false;
                    break;
                }
            }
            if (exist)
                radioLists.add(mcod);

        }
    }


    private void lastUpdate(PullToRefreshBase<ListView> refreshView) {

        String label = DateUtils.formatDateTime(CommunityBroadcastActivity.this, System.currentTimeMillis(),
                DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

        // Update the LastUpdatedLabel
        refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

    }

}
